package com.itbupt.domain.trade.adapter.repository;

import com.itbupt.domain.trade.model.aggregate.GroupBuyOrderAggregate;
import com.itbupt.domain.trade.model.aggregate.GroupBuyTeamSettlementAggregate;
import com.itbupt.domain.trade.model.entity.GroupBuyActivityEntity;
import com.itbupt.domain.trade.model.entity.GroupBuyTeamEntity;
import com.itbupt.domain.trade.model.entity.MarketPayOrderEntity;
import com.itbupt.domain.trade.model.entity.NotifyTaskEntity;
import com.itbupt.domain.trade.model.valobj.GroupBuyProgressVO;

import java.util.List;

/**
 * @Author dingchang
 * @Date 2025/4/28 18:59
 * @Description: 交易服务接口
 */
public interface ITradeRepository {

    // 查询未被支付消费完成的营销优惠订单
    MarketPayOrderEntity queryMarketPayOrderEntityByOutTradeNo(String userId, String outTradeNo);

    // 查询拼团进度
    GroupBuyProgressVO queryGroupBuyProgress(String teamId);

    // 锁定聚合订单
    MarketPayOrderEntity lockMarketPayOrder(GroupBuyOrderAggregate groupBuyOrderAggregate);

    // 查询拼团活动信息 - 构建实体对象
    GroupBuyActivityEntity queryGroupBuyActivityEntityByActivityId(Long activityId);

    // 查询用户在一个拼团活动上参与的次数
    Integer queryOrderCountByActivityId(Long activityId, String userId);

    // 查询组团信息
    GroupBuyTeamEntity queryGroupBuyTeamByTeamId(String teamId);

    // 拼团交易结算
    NotifyTaskEntity settlementMarketPayOrder(GroupBuyTeamSettlementAggregate groupBuyTeamSettlementAggregate);

    // 判断渠道是否有效--是否被拦截
    boolean isSCBlackIntercept(String source, String channel);

    // 查询未执行的回调任务记录
    List<NotifyTaskEntity> queryUnExecutedNotifyTaskList();

    // 查询指定teamId下 未执行的回调任务记录
    List<NotifyTaskEntity> queryUnExecutedNotifyTaskList(String teamId);

    // 更新回调任务状态 为成功1
    int updateNotifyTaskStatusSuccess(String teamId);

    // 更新回调任务状态 为失败3
    int updateNotifyTaskStatusError(String teamId);

    // 更新回调任务状态 为重试2
    int updateNotifyTaskStatusRetry(String teamId);

    /**
     * 乐观锁处理库存以及恢复设置
     * @param teamStockKey 组队库存缓存key
     * @param recoveryTeamStockKey 恢复组队库存缓存key
     * @param target 拼团目标 - 几人成团
     * @param validTime - 拼团的有效时间
     * @return
     */
    boolean occupyTeamStock(String teamStockKey, String recoveryTeamStockKey, Integer target, Integer validTime);

    // 恢复组队库存
    void recoveryTeamStock(String recoveryTeamStockKey, Integer validTime);

}
